package com.bingo.generate.cli.Cmds;


import com.bingo.generate.comm.entity.ColInfo;
import com.bingo.generate.comm.util.FmUtil;
import com.bingo.generate.comm.util.StringHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ClassName    生成sql脚本的命令
 * Description
 * Create by lc
 * Date 2021/7/19 13:37
 */
public class JavaCodeCmd {

    //  返回两张表有数据差异的列
    public static void createEntityFtl(String entityName,List<ColInfo> list){
        String tbName=null;
        if(entityName==null){
            entityName=list.get(0).getTableName();
            //entityName= StringHelper.underlineToCamel(entityName)+"Info";
            entityName= StringHelper.underlineToCamel(entityName);
            entityName=StringHelper.cfLetter(entityName);
            //tbName=list.get(0).getTableName();
        }
        //  设置批量设置属性
        List<List<ColInfo>> groupAttrList=new ArrayList<>();
        {
            //  每组批量设值5个属性
            int batchSetSize=5;
            List<ColInfo> temp=new ArrayList<>();
            for (ColInfo colInfo : list) {
                //  如果达到临界点  重新分组
                if(temp.size()==batchSetSize){
                    groupAttrList.add(temp);
                    temp=new ArrayList<>();
                }
                temp.add(colInfo);
            }
            if(!temp.isEmpty()){
                groupAttrList.add(temp);
            }
        }

        tbName=list.get(0).getTableName();
        Map<String,Object> param=new HashMap<>();
        param.put("modelName",entityName);
        param.put("tbName",tbName);
        param.put("cols",list);
        param.put("groupAttr",groupAttrList);
        //  根据列名进行分组
        FmUtil.printComm("javaTipsAdd.ftl",param);
    }

    //  返回两张表有数据差异的列
    public static void createEntityFtlComm(String entityName,List<ColInfo> list){
        String tbName=null;
        if(entityName==null){
            entityName=list.get(0).getTableName();
            //entityName= StringHelper.underlineToCamel(entityName)+"Info";
            entityName= StringHelper.underlineToCamel(entityName);
            entityName=StringHelper.cfLetter(entityName);
            //tbName=list.get(0).getTableName();
        }
        //  设置批量设置属性
        List<List<ColInfo>> groupAttrList=new ArrayList<>();
        {
            //  每组批量设值5个属性
            int batchSetSize=5;
            List<ColInfo> temp=new ArrayList<>();
            for (ColInfo colInfo : list) {
                //  如果达到临界点  重新分组
                if(temp.size()==batchSetSize){
                    groupAttrList.add(temp);
                    temp=new ArrayList<>();
                }
                temp.add(colInfo);
            }
            if(!temp.isEmpty()){
                groupAttrList.add(temp);
            }
        }

        tbName=list.get(0).getTableName();
        Map<String,Object> param=new HashMap<>();
        param.put("modelName",entityName);
        param.put("tbName",tbName);
        param.put("cols",list);
        param.put("groupAttr",groupAttrList);
        //  根据列名进行分组
        FmUtil.printComm("javaTipsAddComm.ftl",param);
    }

    public static String getType(ColInfo col){
        String type=col.getType();
        if ("int".equals(type)||"tinyint".equals(type)){
            if(col.getColName().toLowerCase().endsWith("id")){
                return "Long";
            }
            return "Integer";
        }
        if("varchar".equals(type)){
            return "String";
        }
        if("datetime".equals(type)){
            return "LocalDateTime";
        }
        if("timestamp".equals(type)){
            return "Date";
        }
        if("double".equals(type)){
            return "Double";
        }
        if ("bigint".equals(type)||"bigint".equals(type)){
            return "Long";
        }
        if("date".equals(type)){
            return "LocalDate";
        }
        if("text".equals(type)){
            return "String";
        }
        if("longtext".equals(type)){
            return "String";
        }
        throw new RuntimeException("该类型未曾处理"+col.getType());
    }

    //  @ApiModelProperty(value = "")
    public static String getAnnotations(ColInfo col){
        StringBuilder strBud=new StringBuilder();
        strBud.append("").append("@ApiModelProperty(value = \""+col.getComment()+"\")");
        if("id".equals(col.getColName())){
            //strBud.append("\n\t").append("@TableId(type = IdType.AUTO)");
            strBud.append("\n\t").append("@TableId");
        }
        /*else{
            strBud.append("\n\t").append("@TableField(value = \""+col.getColName()+"\")");
        }*/
        strBud.append("\n\t").append("@TableColumn(name = \""+col.getColName()+"\" , jdbcType = \""+col.getType()+"\")");
        if(strBud.length()>0){
            strBud.append("\n\t");
        }
        return strBud.toString();
    }

    //  @ApiModelProperty(value = "")
    public static String getCommAnnotations(ColInfo col){
        StringBuilder strBud=new StringBuilder();
        strBud.append("").append("@ApiModelProperty(value = \""+col.getComment()+"\")");
        if("id".equals(col.getColName())){
            //strBud.append("\n\t").append("@TableId(type = IdType.AUTO)");
            strBud.append("\n\t").append("@TableId");
        }
        /*else{
            strBud.append("\n\t").append("@TableField(value = \""+col.getColName()+"\")");
        }*/
        /*strBud.append("\n\t");*/
        if(strBud.length()>0){
            strBud.append("\n\t");
        }
        return strBud.toString();
    }

    //  根据col变成常量
    public static String getConstant(ColInfo col){
        return col.getColName().toUpperCase();
    }

    /**
     * 根据col变成方法头部的常量
     * @param cols
     * @return
     */
    public static String getBatchSetMethodParamStr(List<ColInfo> cols){
        StringBuilder strBud=new StringBuilder();
        for (int i = 0; i < cols.size(); i++) {
            ColInfo col=cols.get(i);
            strBud.append(getType(col)).append(" ").append(toFieldName(col.getColName()));
            if(i!=cols.size()-1){
                strBud.append(" , ");
            }
        }
        return strBud.toString();
    }

    /**
     * 根据要批量设置的内容生成方法头上的注释
     * @param cols
     * @return
     */
    public static String getBatchSetMethodHeadCommentStr(List<ColInfo> cols){
        StringBuilder strBud=new StringBuilder();
        strBud.append("    /**\n");
        strBud.append("     * 批量设值\n");
        for (int i = 0; i < cols.size(); i++) {
            ColInfo col=cols.get(i);
            /*strBud.append(getType(col)).append(" ").append(toFieldName(col.getColName()));
            if(i!=cols.size()-1){
                strBud.append(" , ");
            }*/
            strBud.append("     * @param "+toFieldName(col.getColName())+"\t"+col.getComment()+"\n");
        }
        strBud.append("     */");
        return strBud.toString();
    }

    public static String toFieldName(String colName){
        return StringHelper.underlineToCamel(colName);
    }

    public static String getSqlColStr(List<ColInfo> cols){
        List<String> list=cols.stream().map(ColInfo::getColName).collect(Collectors.toList());
        return String.join(",",list);
    }

    //  返回两张表有数据差异的列
    public static void listSet(String entityName,List<ColInfo> list){
        String tbName=null;
        if(entityName==null){
            entityName=list.get(0).getTableName();
            //entityName= StringHelper.underlineToCamel(entityName)+"Info";
            entityName= StringHelper.underlineToCamel(entityName);
            entityName=StringHelper.cfLetter(entityName);
            //tbName=list.get(0).getTableName();
        }
        tbName=list.get(0).getTableName();
        Map<String,Object> param=new HashMap<>();
        /*param.put("modelName",entityName);
        param.put("tbName",tbName);
        param.put("cols",list);*/
        StringBuilder strBud=new StringBuilder();
        for (ColInfo col : list) {
            String camel=StringHelper.cfLetter(StringHelper.underlineToCamel(col.getColName()));
            String setMethod="entity.set"+camel+"();";
            strBud.append(setMethod).append("\n");
        }
        System.out.println(strBud.toString());
    }

}
